/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package acceso.hibernate;

import acceso.util.Escritor;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author romulogalindo
 */
public class AQuery {

    private Query mquery;
    private String query;
    private long id_session = 0;
    private boolean il_native = false;
    private String log_session = "";

    public AQuery() {
    }

    public AQuery(int id_session) {
        this.id_session = id_session;
        log_session = "[" + this.id_session + "]";
    }

    //Constructor principal de una query
    public AQuery(Query mquery) {
        this.mquery = mquery;
        query = mquery.getQueryString();
    }

    public AQuery(Query mquery, long id_session) {
        this.id_session = id_session;
        this.mquery = mquery;
        query = mquery.getQueryString();
        log_session = "[" + this.id_session + "]";
    }

    public Query getMquery() {
        return mquery;
    }

    public Object uniqueResult() {
        Escritor.escribe_debug(log_session + " " + getQuery());
        long ini = System.currentTimeMillis();
        Object obj = this.mquery.uniqueResult();
        long fin = System.currentTimeMillis();
        Escritor.escribe_info(log_session + " " + getQuery() + "  " + (fin - ini) + "ms");
        return obj;
    }

    public List list() {
        Escritor.escribe_debug(log_session + " " + getQuery());
        long ini = System.currentTimeMillis();
        List obj = this.mquery.list();
        long fin = System.currentTimeMillis();
        Escritor.escribe_info(log_session + " " + getQuery() + "  " + (fin - ini) + "ms");
        return obj;
    }

    public Query getMquery2(int type) {
        switch (type) {
            case 1: {
                Escritor.escribe_frawor(query);
                break;
            }
            case 2: {
                Escritor.escribe_transa(query);
                break;
            }
            case 3: {
                Escritor.escribe_errors(query);
                break;
            }
        }
        return mquery;
    }

    public void setMquery(Query mquery) {
        this.mquery = mquery;

        query = mquery.getQueryString();
    }

    public Query setInteger(String name, int i) {
        query = query.replace(":" + name, "" + i);
        return mquery.setInteger(name, i);
    }

    public Query setString(String name, String v) {
        try {
            query = query.replace(":" + name, "\'" + v + "\'");
        } catch (Exception ep) {
            query = query.replaceAll(":" + name, "\'?\'");
        }
        return mquery.setString(name, v);
    }

    public Query setLong(String name, long l) {
        query = query.replace(":" + name, "" + l);
        return mquery.setLong(name, l);
    }

    public Query setShort(String name, short s) {
        query = query.replace(":" + name, "" + s + "::smallint");
        return mquery.setShort(name, s);
    }

    public Query setDate(String name, Date t) {
        query = query.replace(":" + name, "" + t);
        return mquery.setDate(name, t);
    }

    public Query setTime(String name, Date t) {
        query = query.replace(":" + name, "" + t);
        return mquery.setTime(name, t);
    }

    public Query setTimestamp(String name, Date t) {
        query = query.replace(":" + name, "" + t);
        return mquery.setTimestamp(name, t);
    }

    public Query setBigDecimal(String name, BigDecimal bd) {
        query = query.replace(":" + name, "" + bd);
        return mquery.setBigDecimal(name, bd);
    }

    public String getQuery() {
        return query;
    }

    public Query setShort(String name, Short s) {
        query = query.replace(":" + name, "" + s + "::smallint");
        return mquery.setShort(name, s);
    }

    public boolean isIl_native() {
        return il_native;
    }

    public void setIl_native(boolean il_native) {
        this.il_native = il_native;
    }

}
